package org.mihalovic.PES.Core.Graph.Rank;


import java.util.Collection;

import edu.uci.ics.jung.algorithms.scoring.*;
import edu.uci.ics.jung.graph.DirectedSparseGraph;

import org.mihalovic.PES.Core.Graph.WeightTransformer;
import org.mihalovic.PES.Core.Graph.Edge;
import org.mihalovic.PES.Core.Graph.Node;



/**
 * @author Martin Mihalovic
 */
public class PageRankEvaluator extends AbstractEvaluator<Node, Edge, Double> {
    
    public PageRankEvaluator() {
        this(new DirectedSparseGraph<Node, Edge>());
    }

    public PageRankEvaluator(DirectedSparseGraph<Node, Edge> graph) {
        super();
        SetGraph(graph);
    }


    public void reinitializeEvaluator(DirectedSparseGraph<Node, Edge> graph) {
        evaluator = new PageRank<Node, Edge>(graph, alpha);
        evaluator.setEdgeWeights(new WeightTransformer());
        iterations = 0;
    }


    /** make each vertex hold its score (in RATING property) */
    protected void copyRanksInsideNodes() {
        Collection<Node> nodes = this.graph.getVertices();

        double maxScore=0;

        for(Node n : nodes){
            double score = evaluator.getVertexScore(n);
            if(score > maxScore)
                maxScore=score;

            n.setRank(new PageRankScore(score));
        }
        setMaxRank(maxScore);
    }

    public String toString() {
        return "Page Rank";
    }
}
